// pages/searchHotel/searchHotel.js
let app=getApp();
var mHotelList = [];
var price;
var bedNum;
function HotelBean() {
     var typeId;
     var name;
     var image;
     var price;
     var liveNum;
     var bedNum;
     var avilableNum;
     var score;
     var service;
     var test;
}

Page({

     /**
      * 页面的初始数据
      */
     data: {
          location: '',
          endDate: '',
          startDate: '',
          hotelArray: [],
          loadenable: true,
          shownavindex: 0,
          priceL2H: true,
          dec:false,
          priceSort:false,
          scoreSort:false,
          price:'无',
          bedNum:'无'
     },

     /**
      * 生命周期函数--监听页面加载
      */
     onLoad: function (options) {
          this.setData({
               location: options.location
          });
          var that=this;
          wx.request({
               url:"http://101.37.14.167:8001/RoomType/findByStatus",
               method:"POST",
               header:{'content-type':'application/x-www-form-urlencoded'},
               data:{
                    status:0
               },
               success: function(res){  //若成功，根据返回值生成房间列表
                    mHotelList=[];
                    var list=res.data.data;
                    if(list.length!=0){
                         for(var i=0;i<list.length;i++){
                              var hotelBean=new HotelBean();
                              hotelBean.typeId=list[i].id;
                              hotelBean.name = list[i].name;
                              hotelBean.image=list[i].photo;
                              hotelBean.price = list[i].price;
                              hotelBean.liveNum=list[i].liveNum;
                              hotelBean.bedNum=list[i].bedNum;
                              hotelBean.score = list[i].grade;
                              hotelBean.service = list[i].remark;
                              mHotelList.push(hotelBean);
                         }
                    }
                    that.setData({
                         hotelArray: mHotelList
                    });
               },
               fail:function(res){
                    console.log("失败:"+res.data);
                    wx.showToast({
                         title:'请求失败',
                         icon:'none',
                         duration:2000
                    });
               }
          })
     },
     //推荐排序
     recommendSort:function(){
          mHotelList.sort(function(a,b){
               return (b.score/b.price)-(a.score/a.price);
          })
          this.setData({
               hotelArray: mHotelList
          });
     },
     //价格排序
     priceSort: function(){
          if(this.data.priceSort){
               mHotelList.sort(function(a,b){
                    return b.price-a.price;
               })
               this.setData({
                    priceSort:!this.data.priceSort
               })
          }
          else{
               mHotelList.sort(function(a,b){
                    return a.price-b.price;
               })
               this.setData({
                    priceSort:!this.data.priceSort
               })
          }
          this.setData({
               hotelArray: mHotelList
          });
     },
     //好评排序
     scoreSort:function(){
          if(this.data.scoreSort){
               mHotelList.sort(function(a,b){
                    return a.score-b.score;
               })
               this.setData({
                    scoreSort:!this.data.scoreSort
               })
          }
          else{
               mHotelList.sort(function(a,b){
                    return b.score-a.score;
               })
               this.setData({
                    scoreSort:!this.data.scoreSort
               })
          }
          this.setData({
               hotelArray: mHotelList
          });
     },
     //搜索
     searchEvent:function(e){
          var keyword=e.detail.value;
          let showHotelList=[];
          if(keyword!=''){
               for(var i=0;i<mHotelList.length;i++){
                    if(mHotelList[i].name.indexOf(keyword)>=0){
                         showHotelList.push(mHotelList[i]);
                    }
               }
               if(showHotelList.length==0){
                    wx.showToast({
                         title:'无该房间类型',
                         icon:'none',
                         duration:2000
                    })
                    showHotelList = JSON.parse(JSON.stringify(mHotelList));
               }
          }
          else{
               showHotelList = JSON.parse(JSON.stringify(mHotelList));
          }
          this.setData({
               hotelArray:showHotelList
          })
     },
     filterMenuTap: function (e) {
          var index = e.currentTarget.dataset.index;
          this.setData({
               shownavindex: index
          });
          if(index == 3){
               this.scoreSort();
          }
          else if (index == 2) {
               this.priceSort();
               this.setData({
                    priceL2H:!this.data.priceL2H
               });
          } else {
               this.recommendSort();
               this.setData({
                    priceL2H: true
               });
          }
     },
     //跳转到筛选页面
     filterTap: function () {
          wx.navigateTo({
               url: '../hotelFilter/hotelFilter?price='+this.data.price+'&bedNum='+this.data.bedNum,
          })
     },
     //对价格，床位进行筛选
     onShow: function () {
          price=this.data.price;
          bedNum=this.data.bedNum;
          var flag=0;
          let showHotelList = JSON.parse(JSON.stringify(mHotelList));
          if(price!='无'){
               if(price=='400以上'){
                    for(let i=showHotelList.length-1; i>=0; i--){
                         if(showHotelList[i].price<400){
                              showHotelList.splice(i,1);
                         }
                    }
               }
               else{
                    var a=price.split('-');
                    var min=parseInt(a[0]);
                    var max=parseInt(a[1]);
                    for(let i=showHotelList.length-1; i>=0; i--){
                         var compare=showHotelList[i].price;
                         if(compare<min||compare>max){
                              showHotelList.splice(i,1);
                         }
                    }
               }
               flag=1;
          }
          if(bedNum!='无'){
               bedNum=parseInt(bedNum);
               for(let i=showHotelList.length-1;i>=0;i--){
                    if(showHotelList[i].bedNum!=bedNum)showHotelList.splice(i,1);
               }
               flag=1;
          }
          if(flag==1&&showHotelList.length==0){
               wx.showToast({
                    title:'筛选无结果',
                    icon:'none',
                    duration:2000
               })
          }
          this.setData({
               hotelArray:showHotelList
          })
     },

     locationTap: function () {
          wx.navigateTo({
               url: '../select_city/select_city'
     })
     },
     /**
      * 页面相关事件处理函数--监听用户下拉动作
      */
     onPullDownRefresh: function () {
          var that = this;
          wx: setTimeout(function () {
               wx.request({
                    url:"http://101.37.14.167:8001/RoomType/findByStatus",
                    method:"POST",
                    header:{'content-type':'application/x-www-form-urlencoded'},
                    data:{
                         status:0
                    },
                    success: function(res){  //若成功，根据返回值生成房间列表
                         mHotelList=[];
                         var list=res.data.data;
                         if(list.length!=0){
                              for(var i=0;i<list.length;i++){
                                   var hotelBean=new HotelBean();
                                   hotelBean.typeId=list[i].id;
                                   hotelBean.name = list[i].name;
                                   hotelBean.image=list[i].photo;
                                   hotelBean.price = list[i].price;
                                   hotelBean.liveNum=list[i].liveNum;
                                   hotelBean.bedNum=list[i].bedNum;
                                   hotelBean.score = list[i].grade;
                                   hotelBean.service = list[i].remark;
                                   mHotelList.push(hotelBean);
                              }
                         }
                         that.setData({
                              hotelArray: mHotelList,
                              price:'无',
                              bedNum:'无'
                         });
                    },
                    fail:function(res){
                         console.log("失败:"+res.data);
                         wx.showToast({
                              title:'请求失败',
                              icon:'none',
                              duration:2000
                         });
                    }
               })
               wx.showToast({
                    title: '刷新成功',
                    duration: 1500
               })
               wx.stopPullDownRefresh();
          }, 2000);
     },

     /**
      * 页面上拉触底事件的处理函数
      */
     onReachBottom: function () {
          // var that = this;
          // wx: setTimeout(function () {
          //      mHotelList = [];
          //      // for (var i = 0; i < 10; i++) {
                    // var hotelBean = new HotelBean();
                    // hotelBean.image = 'https://s1.ax1x.com/2020/09/12/wdfSZn.jpg';
                    // hotelBean.name = '皇家海景双床房';
                    // hotelBean.score = 4.5;
                    // hotelBean.service = '停车场/温泉/餐饮/接送';
                    // hotelBean.liveNum=6;
                    // hotelBean.bedNum=3;
                    // hotelBean.price = 329;
                    // mHotelList.push(hotelBean);
                    // //------
                    // var hotelBean = new HotelBean();
                    // hotelBean.image = 'https://s1.ax1x.com/2020/09/12/wdWjMQ.jpg';
                    // hotelBean.name = '豪华海景大床';
                    // hotelBean.score = 4.5;
                    // hotelBean.service = '停车场/温泉/餐饮/接送';
                    // hotelBean.liveNum=6;
                    // hotelBean.bedNum=3;
                    // hotelBean.price = 429;
                    // mHotelList.push(hotelBean);
          //      // }

          //      that.setData({
          //           hotelArray: mHotelList
          //      });
          // }, 2000);

     },

     /**
      * 用户点击右上角分享
      */
     onShareAppMessage: function () {

     }
})